package com.csnf.servlet;

import com.csnf.entity.Task;
import com.csnf.service.TaskService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/task")
public class TaskServlet extends HttpServlet {
    private TaskService taskService = new TaskService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String type = req.getParameter("type");
        Task task = new Task();
        if("find".equals(type))  {
            String task_name = req.getParameter("task_name");
            String task_group = req.getParameter("task_group");
            String task_state = req.getParameter("task_state");
            task.setTask_name(task_name);
            task.setTask_group(task_group);
            task.setTask_state(task_state);
            List<Task> list = taskService.getTask(task);
            req.setAttribute("list",list);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        } else if("add".equals(type)){
            String task_name = req.getParameter("task_name");
            String task_group = req.getParameter("task_group");
            String task_content = req.getParameter("task_content");
            String task_expression = req.getParameter("task_expression");
            String task_strategy = req.getParameter("task_strategy");
            String task_current = req.getParameter("task_current");
            String task_state = req.getParameter("task_state");
            String task_desc = req.getParameter("task_desc");
            task.setTask_name(task_name);
            task.setTask_group(task_group);
            task.setTask_content(task_content);
            task.setTask_expression(task_expression);
            task.setTask_strategy(task_strategy);
            task.setTask_current(task_current);
            task.setTask_state(task_state);
            task.setTask_desc(task_desc);
            int rows = taskService.addTask(task);
            if(rows > 0){
                req.setAttribute("msg","添加成功");
            }else{
                req.setAttribute("msg","添加失败");
            }
            req.getRequestDispatcher("/init").forward(req,resp);
        }
    }
}
